<template>
  <div class="game-menu-overlay" @click="closeMenu">
    <div class="game-menu" @click.stop>
      <div class="menu-header">
        <h2 class="menu-title">
          <span class="menu-icon">🎮</span>
          游戏菜单
        </h2>
        <button @click="closeMenu" class="close-btn">
          <span class="close-icon">✕</span>
        </button>
      </div>
      
      <div class="menu-content">
        <div class="menu-section">
          <h3 class="section-title">游戏操作</h3>
          <div class="menu-buttons">
            <button @click="handleSave" class="menu-btn primary">
              <span class="btn-icon">💾</span>
              <div class="btn-content">
                <span class="btn-label">保存游戏</span>
                <span class="btn-description">保存当前游戏进度</span>
              </div>
            </button>
            
            <button @click="handleLoad" class="menu-btn">
              <span class="btn-icon">📁</span>
              <div class="btn-content">
                <span class="btn-label">加载游戏</span>
                <span class="btn-description">加载已保存的游戏</span>
              </div>
            </button>
            
            <button @click="handleQuickSave" class="menu-btn">
              <span class="btn-icon">⚡</span>
              <div class="btn-content">
                <span class="btn-label">快速保存</span>
                <span class="btn-description">快速保存到默认位置</span>
              </div>
            </button>
            
            <button @click="handleQuickLoad" class="menu-btn">
              <span class="btn-icon">🔄</span>
              <div class="btn-content">
                <span class="btn-label">快速加载</span>
                <span class="btn-description">加载最近的快速保存</span>
              </div>
            </button>
          </div>
        </div>
        
        <div class="menu-section">
          <h3 class="section-title">游戏选项</h3>
          <div class="menu-buttons">
            <button @click="handleSettings" class="menu-btn">
              <span class="btn-icon">⚙️</span>
              <div class="btn-content">
                <span class="btn-label">游戏设置</span>
                <span class="btn-description">配置游戏选项</span>
              </div>
            </button>
            
            <button @click="handleHelp" class="menu-btn">
              <span class="btn-icon">❓</span>
              <div class="btn-content">
                <span class="btn-label">游戏帮助</span>
                <span class="btn-description">查看游戏说明</span>
              </div>
            </button>
            
            <button @click="handleStats" class="menu-btn">
              <span class="btn-icon">📊</span>
              <div class="btn-content">
                <span class="btn-label">游戏统计</span>
                <span class="btn-description">查看游戏数据</span>
              </div>
            </button>
          </div>
        </div>
        
        <div class="menu-section">
          <h3 class="section-title">其他操作</h3>
          <div class="menu-buttons">
            <button @click="handleNewGame" class="menu-btn warning">
              <span class="btn-icon">🆕</span>
              <div class="btn-content">
                <span class="btn-label">新游戏</span>
                <span class="btn-description">开始新的游戏</span>
              </div>
            </button>
            
            <button @click="handleMainMenu" class="menu-btn">
              <span class="btn-icon">🏠</span>
              <div class="btn-content">
                <span class="btn-label">主菜单</span>
                <span class="btn-description">返回主菜单</span>
              </div>
            </button>
            
            <button @click="handleExit" class="menu-btn danger">
              <span class="btn-icon">🚪</span>
              <div class="btn-content">
                <span class="btn-label">退出游戏</span>
                <span class="btn-description">退出到桌面</span>
              </div>
            </button>
          </div>
        </div>
      </div>
      
      <div class="menu-footer">
        <div class="game-info">
          <div class="info-item">
            <span class="info-label">游戏时间:</span>
            <span class="info-value">{{ formatGameTime(gameTime) }}</span>
          </div>
          <div class="info-item">
            <span class="info-label">游戏版本:</span>
            <span class="info-value">v1.0.0</span>
          </div>
        </div>
        
        <button @click="closeMenu" class="footer-btn">
          返回游戏
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useGameStore } from '@/stores'

// Props & Emits
const emit = defineEmits<{
  close: []
  save: []
  load: []
  newGame: []
  exit: []
}>()

// Store
const gameStore = useGameStore()

// Computed
const gameTime = computed(() => gameStore.gameTime || 0)

// Methods
function closeMenu() {
  emit('close')
}

function handleSave() {
  emit('save')
  console.log('保存游戏')
  // 这里应该打开保存对话框或执行保存逻辑
}

function handleLoad() {
  emit('load')
  console.log('加载游戏')
  // 这里应该打开加载对话框
}

function handleQuickSave() {
  console.log('快速保存')
  // 执行快速保存逻辑
  // 可以显示保存成功的通知
}

function handleQuickLoad() {
  console.log('快速加载')
  // 执行快速加载逻辑
}

function handleSettings() {
  console.log('打开设置')
  // 这里可以打开设置界面或发送事件给父组件
}

function handleHelp() {
  console.log('打开帮助')
  // 打开帮助界面或文档
}

function handleStats() {
  console.log('打开统计')
  // 打开游戏统计界面
}

function handleNewGame() {
  if (confirm('确定要开始新游戏吗？当前进度将会丢失！')) {
    emit('newGame')
    console.log('开始新游戏')
  }
}

function handleMainMenu() {
  if (confirm('确定要返回主菜单吗？请确保已保存游戏进度！')) {
    console.log('返回主菜单')
    // 返回主菜单逻辑
  }
}

function handleExit() {
  if (confirm('确定要退出游戏吗？请确保已保存游戏进度！')) {
    emit('exit')
    console.log('退出游戏')
  }
}

function formatGameTime(time: number): string {
  if (time < 60) {
    return `${time} 天`
  } else if (time < 365) {
    const months = Math.floor(time / 30)
    const days = time % 30
    return `${months} 月 ${days} 天`
  } else {
    const years = Math.floor(time / 365)
    const months = Math.floor((time % 365) / 30)
    return `${years} 年 ${months} 月`
  }
}
</script>

<style scoped>
.game-menu-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 2rem;
}

.game-menu {
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  width: 100%;
  max-width: 600px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}

.menu-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.5rem;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-secondary);
  border-radius: 12px 12px 0 0;
}

.menu-title {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text-primary);
}

.menu-icon {
  font-size: 1.5rem;
}

.close-btn {
  width: 40px;
  height: 40px;
  background: none;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.close-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.close-icon {
  font-size: 1.2rem;
}

.menu-content {
  flex: 1;
  padding: 1.5rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.menu-section {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.section-title {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-primary);
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border-color);
}

.menu-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.menu-btn {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem 1.5rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  text-align: left;
  width: 100%;
}

.menu-btn:hover {
  background: var(--bg-hover);
  border-color: var(--primary-color);
  transform: translateY(-1px);
}

.menu-btn.primary {
  background: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
}

.menu-btn.primary:hover {
  background: var(--primary-hover);
}

.menu-btn.warning {
  background: var(--warning-color);
  color: white;
  border-color: var(--warning-color);
}

.menu-btn.warning:hover {
  background: var(--warning-hover);
}

.menu-btn.danger {
  background: var(--error-color);
  color: white;
  border-color: var(--error-color);
}

.menu-btn.danger:hover {
  background: var(--error-hover);
}

.btn-icon {
  font-size: 1.5rem;
  width: 40px;
  text-align: center;
  flex-shrink: 0;
}

.btn-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.btn-label {
  font-weight: 600;
  font-size: 1rem;
  color: inherit;
}

.btn-description {
  font-size: 0.8rem;
  opacity: 0.8;
  color: inherit;
}

.menu-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.5rem;
  border-top: 1px solid var(--border-color);
  background: var(--bg-secondary);
  border-radius: 0 0 12px 12px;
}

.game-info {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.info-item {
  display: flex;
  gap: 0.5rem;
  font-size: 0.9rem;
}

.info-label {
  color: var(--text-secondary);
  font-weight: 500;
}

.info-value {
  color: var(--text-primary);
  font-weight: 600;
}

.footer-btn {
  padding: 0.75rem 1.5rem;
  background: var(--primary-color);
  color: white;
  border: 1px solid var(--primary-color);
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.footer-btn:hover {
  background: var(--primary-hover);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .game-menu-overlay {
    padding: 1rem;
  }
  
  .menu-header {
    padding: 1rem;
  }
  
  .menu-title {
    font-size: 1.25rem;
  }
  
  .menu-content {
    padding: 1rem;
    gap: 1.5rem;
  }
  
  .menu-btn {
    padding: 0.75rem 1rem;
    gap: 0.75rem;
  }
  
  .btn-icon {
    font-size: 1.25rem;
    width: 30px;
  }
  
  .btn-label {
    font-size: 0.9rem;
  }
  
  .btn-description {
    font-size: 0.75rem;
  }
  
  .menu-footer {
    flex-direction: column;
    gap: 1rem;
    align-items: stretch;
    padding: 1rem;
  }
  
  .game-info {
    align-items: center;
  }
  
  .footer-btn {
    width: 100%;
    text-align: center;
  }
}

/* 动画效果 */
@keyframes slideIn {
  from {
    opacity: 0;
    transform: scale(0.9) translateY(-20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.game-menu {
  animation: slideIn 0.3s ease-out;
}

.menu-btn {
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.menu-btn:active {
  transform: translateY(0) scale(0.98);
}
</style>